﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DbSharp.Framework.Storage;
using System.IO;
using System.Xml.Linq;
using DbSharp.Framework.Storage.IO;

namespace DbSharp.Framework.Diff
{
    public class HtmlSetDiffOutputer : ISetDiffOutputer
    {

        private void Prepare(string outputFileName)
        {
            if (File.Exists(outputFileName))
                File.Delete(outputFileName);
        }


        #region ISetDiffOutputer Members

        public void WriteChangedLines(string outputFileName, string title, HashSet<LegendDisplayFlag> legendFlag, List<string> fieldsOutputNameList, IEnumerable<XElement> addedLines, IEnumerable<XElement> deletedLines)
        {
            IList<string> addedStringLines;
            IList<string> deletedStringLines;

            var textWriter = new RecordsetXElmtTextWriter(addedLines, false, -1);
            addedStringLines = textWriter.StringList;
            textWriter = new RecordsetXElmtTextWriter(deletedLines, false, -1);
            deletedStringLines = textWriter.StringList;

            string diffText = DiffHtmlOutputHelper.ComposeHtml_ChangedLines(title, legendFlag, addedStringLines, deletedStringLines);
            Prepare(outputFileName);
            File.WriteAllText(outputFileName, diffText, Encoding.UTF8);
        }

        public void WriteCommonLines(string outputFileName, string title, HashSet<LegendDisplayFlag> legendFlag, List<string> fieldsOutputNameList, IEnumerable<XElement> commonLines)
        {
            IList<string> commonStringLines;

            var textWriter = new RecordsetXElmtTextWriter(commonLines, false, -1);
            commonStringLines = textWriter.StringList;

            string diffText = DiffHtmlOutputHelper.ComposeHtml_CommonLines(title, legendFlag, commonStringLines);
            Prepare(outputFileName);
            File.WriteAllText(outputFileName, diffText, Encoding.UTF8);
        }

        public void WriteUnionLines(string outputFileName, string title, HashSet<LegendDisplayFlag> legendFlag, List<string> fieldsOutputNameList, IEnumerable<XElement> commonLines, IEnumerable<XElement> addedLines, IEnumerable<XElement> deletedLines)
        {
            IList<string> addedStringLines;
            IList<string> deletedStringLines;
            IList<string> commonStringLines;

            var textWriter = new RecordsetXElmtTextWriter(addedLines, false, -1);
            addedStringLines = textWriter.StringList;
            textWriter = new RecordsetXElmtTextWriter(deletedLines, false, -1);
            deletedStringLines = textWriter.StringList;
            textWriter = new RecordsetXElmtTextWriter(commonLines, false, -1);
            commonStringLines = textWriter.StringList;

            string diffText = DiffHtmlOutputHelper.ComposeHtml_UnionLines(title, legendFlag, addedStringLines, deletedStringLines, commonStringLines);
            Prepare(outputFileName);
            File.WriteAllText(outputFileName, diffText, Encoding.UTF8);
        }

        #endregion

 
    }


    public class HtmlTextLineDiffOutputer : ITextLineDiffOutputer
    {
        private void Prepare(string outputFileName)
        {
            if (File.Exists(outputFileName))
                File.Delete(outputFileName);
        }

        #region ITextLineDiffOutputer Members


        public void WriteRevisionsDiff(string outputFileName, string title, string rev1, string rev2)
        {
            string diffText = DiffHtmlOutputHelper.ComposeHtml_RevisionsDiff(title, rev1, rev2);
            Prepare(outputFileName);
            File.WriteAllText(outputFileName, diffText, Encoding.UTF8);
        }

        #endregion
    }
}
